package Model.DateProfile;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DBAccess.DBAccess;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/editprofile")
public class EditProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Properties props = new Properties(); 

	 @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 new DBAccess();
		 String name = request.getParameter("name");
			String address = request.getParameter("address");
			int gender = Integer.parseInt(request.getParameter("gender"));
			int height = Integer.parseInt(request.getParameter("height"));
			int weight = Integer.parseInt(request.getParameter("weight"));
			int year =  Integer.parseInt(request.getParameter("year"));
			int month = Integer.parseInt(request.getParameter("month"));
			int date = Integer.parseInt(request.getParameter("date"));
			System.out.println(date);
			Date birthdate = new Date(year,month,date);
			String[] interests = request.getParameterValues("interests");
			int orientation = Integer.parseInt(request.getParameter("orientation"));
			String religion = request.getParameter("religion");
			String ethnicity = request.getParameter("ethnicity");
			int userID = Integer.parseInt(request.getParameter("userID"));
			System.out.println("servlet:"+request.getParameter("newsletter"));
			int newsletter = Integer.parseInt(request.getParameter("newsletter"));
			System.out.println("servlet:"+request.getParameter("notification"));
			int notification = Integer.parseInt(request.getParameter("notification"));

			String interestsString = null;
			
			if(interests != null){
				StringBuffer buf = new StringBuffer();
				for (int i=0; i<interests.length; i++){
					buf.append(interests[i]+";");
				}
				interestsString = buf.toString();
			}
			System.out.printf("%s %s %s %s %s %s %s %s %s\n", 
					name,gender,height,weight,birthdate.toString(),
					interestsString,orientation,religion,request.getParameter("userID"));
			DBAccess.updateName(name, userID);
			DBAccess.updateInfo(userID, address, height, weight, year,month,date, interestsString, gender, orientation, newsletter, notification, ethnicity, religion);
			response.sendRedirect("profile.jsp");
			return;

		}

}
